home *** CD-ROM | disk | FTP | other *** search
/ AOL File Library: 2,801 to 2,900 / aol-file-protocol-4400-2801-to-2900.zip / AOLDLs / C++ Files Library / Graphic Gems I, II & III (C_C++) / Graphics Gems C Code.sea / GemsII / Peano / mapply.c < prev    next >
C/C++ Source or Header  |  1992-06-16  |  2KB  |  84 lines

  1. /* mapply.c */
  2. /*
  3.  * maps an 8-bit image into a 24-bit image,
  4.  * using color map in a user-specified file
  5.  *
  6.  * color maps may be generated using ran_ramp
  7.  */
  8.  
  9. /* Ken Musgrave */
  10. /* June 1986 */
  11.  
  12.  
  13.  
  14. #include <stdio.h>
  15. #include <math.h>
  16. #include "types.h"
  17.  
  18. char           *image_file = "img";
  19.  
  20. int             r, g, b;
  21. unsigned char   eight_bit[FB_SIZE], twenty_four_bit[FB_SIZE][3], cmap[256][3];
  22.  
  23. FILE           *imfile, *cmapfile, *outfile, *fopen();
  24.  
  25.  
  26. main(argc, argv)
  27. int             argc;
  28. char           *argv[];
  29. {
  30.     int             i, x, y;
  31.  
  32.         /* get command-line args */
  33.     if (argc != 3) {
  34.         fprintf(stderr, "usage: %s infile cmap_file\n", argv[0]);
  35.         exit(0);
  36.     }
  37.     if ((imfile = fopen(argv[1], "r")) == NULL) {
  38.         fprintf(stderr, "Error: cannot open file %s\n", argv[1]);
  39.         exit(-1);
  40.     }
  41.     if ((cmapfile = fopen(argv[2], "r")) == NULL) {
  42.         fprintf(stderr, "Error: cannot open file %s\n", argv[2]);
  43.         exit(-1);
  44.     }
  45.     if ((outfile = fopen(image_file, "w")) == NULL) {
  46.         fprintf(stderr, "Error: cannot open file %s\n", image_file);
  47.         exit(-1);
  48.     }
  49.     printf("%s: mapping \"%s\" to \"%s\" using map in \"%s\"\n", 
  50.             argv[0], argv[1], image_file, argv[2]);
  51.  
  52.     fscanf(cmapfile, "%d %d\n", &r, &g);
  53.     for (i = 0; i < 256; i++) {
  54.         fscanf(cmapfile, "%d %d %d\n", &r, &g, &b);
  55.         cmap[i][0] = (unsigned char) r;
  56.         cmap[i][1] = (unsigned char) g;
  57.         cmap[i][2] = (unsigned char) b;
  58.     }
  59.     cmap[0][0] = cmap[1][0];
  60.     cmap[0][1] = cmap[1][1];
  61.     cmap[0][2] = cmap[1][2];
  62.     cmap[255][0] = cmap[254][0];
  63.     cmap[255][1] = cmap[254][1];
  64.     cmap[255][2] = cmap[254][2];
  65.  
  66.     for (y = 0; y < FB_SIZE; y++) {
  67.         fread(eight_bit, 1, FB_SIZE, imfile);
  68.         for (x = 0; x < FB_SIZE; x++) {
  69.             if (eight_bit[x]) {
  70.                 twenty_four_bit[x][0] = cmap[eight_bit[x]][0];
  71.                 twenty_four_bit[x][1] = cmap[eight_bit[x]][1];
  72.                 twenty_four_bit[x][2] = cmap[eight_bit[x]][2];
  73.             } else {
  74.                 twenty_four_bit[x][0] = 0;
  75.                 twenty_four_bit[x][1] = 0;
  76.                 twenty_four_bit[x][2] = 0;
  77.             }
  78.         }
  79.         fwrite(twenty_four_bit, 3, FB_SIZE, outfile);
  80.     }
  81.  
  82. } /* main() */
  83.  
  84.